package com.inxedu.os.edu.controller.user;


import com.inxedu.os.edu.pojo.SysLoginLog;
import com.inxedu.os.edu.pojo.SysRole;
import com.inxedu.os.edu.pojo.SysUser;
import com.inxedu.os.edu.pojo.result;
import com.inxedu.os.edu.service.user.AdminSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller

public class AdminUserController {
    @Autowired
    private AdminSysUser adminSysUser;
    @InitBinder({"sysUser"})
    public void initBinderSysFunction(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysUser.");
    }
    //查找角色列表
    @RequestMapping("admin/sysuser/userlist")
    public  String tosysuser(Model model ,@ModelAttribute("keyWord")String keyWord){
        SysUser sysUser=new SysUser();
        sysUser.setLoginName(keyWord);
        sysUser.setEmail(keyWord);
        List<SysUser> select = adminSysUser.select(sysUser);
        model.addAttribute("userList",select);
        List<SysRole> sysRoles = adminSysUser.selectRole();
        model.addAttribute("sysRoleList",sysRoles);
        return "view/inxedu/admin/system/sys-user-list";
    }
    //伪删除
    @RequestMapping("/admin/sysuser/disableOrstart/{userId}/{type}")
    @ResponseBody
    public result updatesysuser(Model model, @PathVariable("userId")int userId, @PathVariable("type") int type) {

        adminSysUser.updatestatus(userId,type);
        result result=new result();
        result.setSuccess(true);
        return result;
    }
    //点击冻结隐藏
    @RequestMapping("/admin/sysuser/updateuser")
    @ResponseBody
    public result updatausers(Model model, @ModelAttribute("sysUser") SysUser sysUser) {
        adminSysUser.updatauser(sysUser);
        result result = new result();
        result.setSuccess(true);
        result.setMessage("修改成功");
        return result;
    }

    //跳转日志
    @RequestMapping("/admin/sysuser/looklog/{userId}")
    public String tolooklog(@PathVariable("userId") Integer userId,Model model){
        System.out.println(userId);
        List<SysLoginLog> sysLoginLogs = adminSysUser.selectByExample(userId);
        System.out.println(sysLoginLogs);
        model.addAttribute("logList",sysLoginLogs);
        return "view/inxedu/admin/system/sys-user-loginlog";
    }
    //修改
    @RequestMapping("/admin/sysuser/initupdateuser/{userId}")
    @ResponseBody
    public result update(Model model,@ModelAttribute("userId")int userId){
        SysUser user = adminSysUser.selectAll(userId);
        System.out.println(user);

//      adminSysUser.updatauser(user1);
        result result=new result();
        result.setSuccess(true);
        result.setEntity(user);
        return result;
    }
    //修改密码
    @RequestMapping("/admin/sysuser/updatepwd/{userId}")
    @ResponseBody
    public result updadapwd(HttpServletRequest request, @PathVariable("userId") int userId) {
        String newPwd = request.getParameter("newPwd");
        adminSysUser.updadauserpwd(userId, newPwd);
        result result = new result();
        result.setSuccess(true);
        result.setMessage("修改成功");
        return result;
    }
    //新增
@RequestMapping("/admin/sysuser/createuser")
@ResponseBody
public result insetsysuser(Model model, @ModelAttribute("sysUser") SysUser sysUser) {
    System.out.println(sysUser);
    adminSysUser.insetsysuser(sysUser);
    result result = new result();
    result.setSuccess(true);
    result.setMessage("创建成功");
    return result;
}
}
